package com.knowledge.plus.多线程.锁.显示锁;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 可重入锁：同一线程可重复获取同一把锁（递归调用不会死锁）
 * 特性：可重入、支持公平/非公平策略、可中断、可设置超时
 *
 * @author TPF
 * @since 2025/6/9
 */
public class ReentrantLockDemo {

    private final Lock lock = new ReentrantLock(); // 默认非公平锁
    private int count = 0;

    public void increment() {
        lock.lock(); // 获取锁
        try {
            count++;
        } finally {
            lock.unlock(); // 确保锁释放
        }
    }

}
